# 连接mongo
require 'mongo'
client = Mongo::Client.new('mongodb://localhost:27017/mydb')
collection = client[:mycollection]
# collection.create
# 插入文档
# result = collection.insert_one({ name: 'Mark', age: 36 })
cursor = collection.find.to_a
# puts result.to_a
puts cursor

# 更新文档
# result = collection.update_many({ name: 'Hary' }, { '$set' => { age: 100 } })
puts collection.find.to_a
# 删除集合
# collection.delete_many({name:'Jack'})
# puts collection.find.to_a 
# 插入多条文档
collection.insert_many([{name:'Hary',age:26},{name:'Lucy',age:22}])
puts collection.find.to_a 
puts "------------- find data of equal 26 --------------------"
puts collection.find({age:{'$eq':26}}).to_a
puts "------------- find data of no equal 26 --------------------"
puts collection.find({age:{'$ne':26}}).to_a
puts "------------- find data of equal 26 --------------------"
puts collection.find({age:{'$gt':26}}).to_a
puts "------------- find data of greater than equal 26--------------------"
puts collection.find({age:{'$gte':26}}).to_a
puts "------------- find data of less than 26--------------------"
puts collection.find({age:{'$lt':26}}).to_a
puts "------------- find data of less than equal 26--------------------"
puts collection.find({age:{'$lte':26}}).to_a
puts "------------- find data of unless equal 26"
puts equal26 = collection.find({age:{'$eq':26}}).first()['_id']
puts collection.find({age:{'$ne':equal26}}).to_a
puts "------------- find data of equal 26 and equal 30"
puts equal26 = collection.find({age:{'$eq':26}}).first()['_id']
puts collection.find({age:{'$ne':equal26}}).to_a
